package vload

import (
	"reflect"
)

// LoadQuick 快速加载单一分组的值映射
func LoadQuick[K comparable, V any](
	data interface{},
	sourceField string,
	targetField string,
	getter Getter[K, V],
) error {
	return LoadQuickWithType(data, sourceField, targetField, reflect.Invalid, reflect.Invalid, getter)
}

// LoadQuickWithType 带类型指定的快速加载方法
func LoadQuickWithType[K comparable, V any](
	data interface{},
	sourceField string,
	targetField string,
	sourceType reflect.Kind,
	targetType reflect.Kind,
	getter Getter[K, V],
) error {
	loader := New()
	loader.AddMapping("default", []Pair{
		{
			SourceField: sourceField,
			TargetField: targetField,
			SourceType:  sourceType,
			TargetType:  targetType,
		},
	}, getter)
	return loader.Load(data)
}

// FieldPair 字段映射对
type FieldPair struct {
	Source string
	Target string
}

// LoadQuickMulti 快速加载多个字段的值映射
func LoadQuickMulti[K comparable, V any](
	data interface{},
	fields []FieldPair,
	getter Getter[K, V],
) error {
	return LoadQuickMultiWithType(data, fields, reflect.Invalid, reflect.Invalid, getter)
}

// LoadQuickMultiWithType 带类型指定的多字段快速加载方法
func LoadQuickMultiWithType[K comparable, V any](
	data interface{},
	fields []FieldPair,
	sourceType reflect.Kind,
	targetType reflect.Kind,
	getter Getter[K, V],
) error {
	loader := New()
	pairs := make([]Pair, len(fields))
	for i, field := range fields {
		pairs[i] = Pair{
			SourceField: field.Source,
			TargetField: field.Target,
			SourceType:  sourceType,
			TargetType:  targetType,
		}
	}
	loader.AddMapping("default", pairs, getter)
	return loader.Load(data)
}
